import { defineStore } from 'pinia'
import { request } from '@/utils/request'
import { tokenManager } from '@/utils/tokenManager';
// const token = 'pat_hDLUMqRTuu6iyZo25QTCKqK07KkRwh6JiQiroJZSUqdKlDnwrMF6PE1LI5POPB70'
const workflow_img_id = '7466274557663789067';
const token = 'pat_hDLUMqRTuu6iyZo25QTCKqK07KkRwh6JiQiroJZSUqdKlDnwrMF6PE1LI5POPB70'; //2025/3/1过期
// const workflow_id = '7466775571867959346';
// const url = `https://api.coze.cn/v1/workflow/run`;
// const headers = {
//     'Content-Type': 'application/json',
//     'Authorization': `Bearer ${token}`,
// };

export const useSongStore = defineStore('songs', {
    state: () => ({
        songs: [],
        loading: false
    }),
    actions: {
        async fetchSongs() {
            this.loading = true
            try {
                const { data } = await request.get('/songs')
                console.log("data", data)
                this.songs = data
            } catch (error) {
                console.error('获取失败:', error)
                throw error
            } finally {
                this.loading = false
            }
        },
        async addSongs(data) {
            this.loading = true
            try {
                const { res } = await request.post('/songs', data)
                console.log("res", res)
            } catch (error) {
                console.error('获取失败:', error)
                throw error
            } finally {
                this.loading = false
            }
        },
        async updateSong(song) {
            const response = await request.put(`/songs/${song.id}`, song);
            console.log("response", response)
            // const index = this.songs.value.findIndex(s => s.id === song.id);
            // if (index !== -1) {
            //     this.songs.value[index] = response.data;
            // }
        },
        async deleteSong(id) {
            this.loading = true
            try {
                await request.delete(`/songs/${id}`)
                // 删除成功后更新本地数据
                this.songs = this.songs.filter(song => song.id !== id)
                uni.showToast({
                    title: '删除成功',
                    icon: 'success'
                })
            } catch (error) {
                console.error('删除失败:', error)
                uni.showToast({
                    title: '删除失败',
                    icon: 'error'
                })
                throw error
            } finally {
                this.loading = false
            }
        },
        async createImg(parameters) {
            try {
                const body = {
                    // parameters: {
                        // desc: imgFormData.content,
                        // total: Number(imgFormData.count),
                        // scene: imgFormData.scene
                    // },
                    parameters: parameters,
                    workflow_id: workflow_img_id,
                    is_async: false,
                };
    
                const response = await uni.request({
                    url: tokenManager.getCozeApiUrl(),
                    method: 'POST',
                    header: tokenManager.getHeaders(), // 添加请求头
                    data: body,
                });
    
                if (response.statusCode !== 200) {
                    throw new Error(`HTTP error! status: ${response.statusCode}`);
                }
    
                const obj = response.data;
    
                console.log('obj', obj);
                const aim = JSON.parse(obj.data)
                console.log('aim', aim);
                return aim.output.filter(item => !!item);
                // list.value[currentTab.value]['imgs'][curImgIndex.value].push(...aim.output)
                // console.log('list', list.value[currentTab.value]['imgs'])
    
            } catch (error) {
                console.error('Error:', error);
                uni.showToast({ title: '请求失败', icon: 'none' });
            }
            return []
        }

    }
})